#define _CRT_SECURE_NO_WARNINGS 1
#include"Head.h"
class Solution {
public:
    vector<int> intersection(vector<int> nums1, vector<int> nums2) {
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());
        int p1 = 0, p2 = 0;
        set<int> ans;
        while (p1 < nums1.size() && p2 < nums2.size())
        {
            if (nums1[p1] == nums2[p2])
            {
                ans.insert(nums1[p1]);
                p1++;
                p2++;
            }
            else
            {
                nums1[p1] < nums2[p2] ? p1++ : p2++;
            }
        }
        vector<int> ret(ans.begin(), ans.end());
        return ret;
    }
};

int main()
{
    Solution s;
    vector<int> ans = s.intersection({ 4,9,5 }, { 9,4,9,8,4 });
    for (auto e : ans)
    {
        cout << e << " ";
    }
    cout << endl;
    return 0;
}